package person.bing.fbcloud.auth.config;

import lombok.AllArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.http.HttpMethod;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.redis.RedisTokenStore;
import person.bing.fbcloud.auth.constant.FBCloudSecurityConstants;

import javax.sql.DataSource;

/**
 * oauth2配置
 * @author: mengfanbing
 * @date: 2020/1/14 18:18
 */
@Configuration
@AllArgsConstructor
@EnableAuthorizationServer
public class AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter {
    private final DataSource dataSource;
    private final AuthenticationManager authenticationManagerBean;
    private final RedisConnectionFactory redisConnectionFactory;
    private final FbcloudUserService fbcloudUserService;


    //客户端认证
    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        FBCloudClientDetailsService fbCloudClientDetailsService = new FBCloudClientDetailsService(dataSource);
        fbCloudClientDetailsService.setFindClientDetailsSql(FBCloudSecurityConstants.DEFAULT_FIND_STATEMENT);
        fbCloudClientDetailsService.setSelectClientDetailsSql(FBCloudSecurityConstants.DEFAULT_SELECT_STATEMENT);
        clients.withClientDetails(fbCloudClientDetailsService);
    }

    //用户认证
    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        endpoints
                .allowedTokenEndpointRequestMethods(HttpMethod.GET,HttpMethod.POST)
                .userDetailsService(fbcloudUserService)
                .tokenStore(tokenStore())
                .authenticationManager(authenticationManagerBean);
    }

    @Override
    public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception {
        oauthServer
                .allowFormAuthenticationForClients()//允许表单认证
                .checkTokenAccess("isAuthenticated()")// 开启/oauth/check_token验证端口认证权限访问
        ;
    }
    @Bean
    public TokenStore tokenStore(){
        RedisTokenStore redisTokenStore = new RedisTokenStore(redisConnectionFactory);
        redisTokenStore.setPrefix(FBCloudSecurityConstants.TKCLOUD_PREFIX + FBCloudSecurityConstants.OAUTH_PREFIX);
        return redisTokenStore;
    }

}

